﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ciiattack
{
    public class Player : Actor
    {
        MissileManager missileManager;
        EnemyManager enemyManager;
        public int score;
        private SpriteFont _scr;

        public SpriteFont Scr
        {
            get { return _scr; }
            set { _scr = value; }
        }

        public Player(Texture2D texture, Vector2 origin, SpriteBatch spriteBatch, MissileManager missileManager, EnemyManager enemyManager)
            : base(texture, origin, spriteBatch, new Vector2(120.0f, 416.0f))
        {
            this.missileManager = missileManager;
            this.enemyManager = enemyManager;
        }

        public override int Update(GameTime gameTime, SpriteFont scr, int resultat)
        {
            KeyboardState keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.Left))
            {
                if (this.Position.X > 32.0f)
                {
                    this.Position -= 4.0f * Vector2.UnitX;
                }
            }

            if (keyboardState.IsKeyDown(Keys.Right))
            {
                if (this.Position.X < 208.0f)
                {
                    this.Position += 4.0f * Vector2.UnitX;
                }
            }
            base.Update(gameTime);
            if (keyboardState.IsKeyDown(Keys.Space))
            {
                this.missileManager.Fire(this.Position);
            }
            // Collisions...
            foreach (Enemy e in this.enemyManager.Enemies)
            {
                if (this.BoundingRectangle.Intersects(e.BoundingRectangle))
                {
                    resultat = 0;

                    this.spriteBatch.Begin();
                    this.spriteBatch.DrawString(scr, resultat.ToString(), new Vector2(10, 20), Color.White);
                    this.spriteBatch.End();
                    e.OnHit(); // Tell the Enemy that it has been hit!
                    break; // One collision is enough.
                }
            }
            return resultat;
        }
    }
}